//
//  ViewController.m
//  ObjcInteractiveForJS
//
//  Created by YTYanK on 17/4/6.
//  Copyright © 2017年 YTYanK. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <WKWebViewJavascriptBridge.h>
#import "ObjcInteractiveForJS-Swift.h"


@interface ViewController ()<WKNavigationDelegate>
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) UIButton *btn;
@property (weak, nonatomic) UITextView *textView;
@property (nonatomic) WKWebViewJavascriptBridge *bridge;
@property (nonatomic) WKWebView *sobWebView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame];
    webView.backgroundColor = [UIColor redColor];
    webView.userInteractionEnabled = YES;
    webView.navigationDelegate = self;
    _sobWebView = webView;
    
    
    
    [WKWebViewJavascriptBridge enableLogging];  // 开启日志
    _bridge =  [WKWebViewJavascriptBridge bridgeForWebView:webView];// 初始化，建立沟通渠道
  
    [_bridge setWebViewDelegate:self]; // 需要开启代理
    

    
    [_bridge registerHandler:@"getC" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"反馈过来啊---->%@", data);
        // 反馈给JS
        responseCallback(@{@"userId":@"123456"});
    }];
    
    
    [_bridge registerHandler:@"getABC" handler:^(id data, WVJBResponseCallback responseCallback) {
       
        NSLog(@"JS 返回的内容%@",data);
        responseCallback(@"JS 调用了 OC");
    }];
    

    [self loadExamplePage:webView];
    
    [self.view addSubview:webView];

    [self renderButtons:webView];
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// web
- (void)renderButtons:(WKWebView*)webView {
    
    CGFloat y = self.view.frame.size.height - 35;
    UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:11.0];
    
    UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [callbackButton setTitle:@"Call handler" forState:UIControlStateNormal];
    [callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:callbackButton aboveSubview:webView];
    callbackButton.frame = CGRectMake(0, y, 100, 35);
    callbackButton.titleLabel.font = font;
    
    UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [reloadButton setTitle:@"Reload webview" forState:UIControlStateNormal];
    [reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:reloadButton aboveSubview:webView];
    reloadButton.frame = CGRectMake(90, y, 100, 35);
    reloadButton.titleLabel.font = font;
    
    UIButton* safetyTimeoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [safetyTimeoutButton setTitle:@"Disable safety timeout" forState:UIControlStateNormal];
    [safetyTimeoutButton addTarget:self action:@selector(disableSafetyTimeout) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:safetyTimeoutButton aboveSubview:webView];
    safetyTimeoutButton.frame = CGRectMake(190, y, 120, 35);
    safetyTimeoutButton.titleLabel.font = font;
    
    
    UIButton* gotoPhotoBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [gotoPhotoBtn setTitle:@"前往照相机" forState:UIControlStateNormal];
    [gotoPhotoBtn addTarget:self action:@selector(gottoPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:gotoPhotoBtn aboveSubview:webView];
    gotoPhotoBtn.frame = CGRectMake(280, y, 120, 35);
    gotoPhotoBtn.titleLabel.font = font;
    
}

- (void)disableSafetyTimeout {
    [self.bridge disableJavscriptAlertBoxSafetyTimeout];
}


- (void)reloadWeb:(id)sender {
    [_sobWebView reload];
}

- (void)gottoPhoto:(id)sender {

}

- (void)callHandler:(id)sender {
    id data = @{ @"OBJC": @"OC 调了用 JS!" };
    [_bridge callHandler:@"cellJS" data:data responseCallback:^(id response) {
        NSLog(@"testJavascriptHandler responded: %@", response);
    }];
}

// 获取文件内的html文件
- (void)loadExamplePage:(WKWebView *)webView {
    NSError *err = nil;
    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&err];
    if (err == nil) {
        NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
        [webView loadHTMLString:appHtml baseURL:baseURL];
    }
   
}


- (UILabel *)text {
    if (_text == nil) {
        _text = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 50, self.view.frame.size.height, 100, 100)];
        _text.text = @"摄制什么啊";
    }
    return _text;
}


@end
